﻿using GetSmartIDEList.Lib.Common;
using GetSmartIDEList.WebAPI.Models;
using Newtonsoft.Json;
using System.Text;

namespace GetSmartIDEList.WebAPI.Common
{
    public static class GetAPIAuthor
    {
        public static string GetToken()
        {
            AuthToken authToken = new AuthToken();
            //JSON传参
            //string json = JsonConvert.SerializeObject(authToken);

            //键值对传参
            var dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(authToken));
            StringBuilder builder = new StringBuilder();
            foreach (var item in dic)
            {
                builder.AppendFormat("{0}={1}&", item.Key, item.Value);
            }

            string url = "https://gitee.com/oauth/token";

            //获取授权
            //HttpPost.Post(url, builder.ToString(), out outToken, "application/x-www-form-urlencoded", Encoding.GetEncoding("utf-8"), null, true, null);

            string resultReq = HttpPost.HttpWebRequest(url, builder.ToString().TrimEnd('&'), Encoding.GetEncoding("utf-8"), "application/x-www-form-urlencoded");

            string tokenStr = "";
            if (resultReq != null)
            {
                //转换成Json并从对象中拿到token
            }

            return tokenStr;
        }
    }
}
